package com.ruoyi.web.controller.vidiomonitor;
/**
 * ClassName: AreaInfoController <br/>
 * Description: <br/>
 * date: 2020/12/31 09:32<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.hikvisionutils.HikVisionUtils;
import com.ruoyi.common.utils.hikvisionutils.model.*;
import com.ruoyi.common.utils.hikvisionutils.model.Batch;
import com.ruoyi.system.domain.Regions;
import com.ruoyi.system.service.IRegionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @program: ruoyi
 * @description: 区域信息相关接口
 * @author: hesion
 * @create: 2020-12-31 09:32
 **/
@Api("区域信息相关接口。")
@RestController
@RequestMapping("/videoapi/areaInfo")
public class AreaInfoController {
    @Autowired
    IRegionsService regionsService;
    @Autowired
    private IRegionsService regionsServices;
    /**
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Author hst
     * @Description //TODO 获取根区域信息
     * @Date 18:01 2020/12/30
     * @Param []
     **/
    @ApiOperation("获取根区域信息")
    @RequestMapping(value = "/regionsRoot", method = RequestMethod.POST)
    public AjaxResult getRegionsRoot() {
        String body = HikVisionUtils.getRegionsRoot().getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }

    @ApiOperation("查询区域列表v2")
    @ApiImplicitParam(name = "nodesByParams", value = "请求的参数对象", dataType = "NodesByParams")
    @RequestMapping(value = "/nodesByParams", method = RequestMethod.POST)
    public AjaxResult getNodesByParams(@RequestBody NodesByParams nodesByParams) {
        String body = HikVisionUtils.getNodesByParams(nodesByParams).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }

    /**
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Author hst
     * @Description //TODO 根据区域获取下级资源列表v2
     * @Date 15:50 2020/12/30
     * @Param [subResouce]
     **/
    @ApiOperation("根据区域编号获取下一级区域列表v2")
    @ApiImplicitParam(name = "regionsByPage", value = "请求的参数对象", dataType = "RegionsByPage")
    @RequestMapping(value = "/subRegion", method = RequestMethod.POST)
    public AjaxResult getSubResourceByRegion(@RequestBody SubRegion subRegion) {
        String body = HikVisionUtils.getSubRegionByRegion(subRegion).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }

    /**
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Author hst
     * @Description //TODO 获取区域列表接口可用来全量同步区域信息，返回结果分页展示。
     * @Date 15:28 2020/12/30
     * @Param [regionsByPage]
     **/
    @ApiOperation("分页获取区域列表")
    @ApiImplicitParam(name = "regionsByPage", value = "请求的参数对象", dataType = "RegionsByPage")
    @RequestMapping(value = "/regionsPage", method = RequestMethod.POST)
    public AjaxResult getRegionsByPage(@RequestBody RegionsByPage regionsByPage) {
        String body = HikVisionUtils.getRegionsByPage(regionsByPage).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }

    @ApiOperation("获取单个区域信息")
    @ApiImplicitParam(name = "singleRegionRequest", value = "请求的参数对象", dataType = "SingleRegionRequest")
    @RequestMapping(value = "/singleRegion", method = RequestMethod.POST)
    public AjaxResult getSingleRegionInfo(@RequestBody SingleRegionRequest singleRegionRequest) {
        String body = HikVisionUtils.getSingleRegionInfo(singleRegionRequest).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("添加单个区域信息")
    @ApiImplicitParam(name = "add", value = "请求的参数对象", dataType = "Batch")
    @RequestMapping(value = "/batch/add", method = RequestMethod.POST)
    public AjaxResult add(@RequestBody Batch[] batch) {
        String body = HikVisionUtils.getBatch(batch).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }

    //更新数据
    public void setRegionsService(HIKResponseResult hikResponseResult) {
        List<Map<String,String>> map= (List<Map<String, String>>) hikResponseResult.getData().get("list");
        for (int i = 0; i <map.size(); i++) {
            Regions regions= new Regions();
            regions.setName(map.get(i).get("name"));
            regions.setIndexcode(map.get(i).get("indexCode"));
            regions.setParentindexcode(map.get(i).get("parentIndexCode"));
            regionsService.insertRegions(regions);
        }
    }
}
